<?php //global $modx; ?>

<h2 id="newentry_header_<?php echo $plural_name ?>" class="clickable">
	 &nbsp; <a><span style="color: green;font-weight: bold;"> + </span>  Add New <?php echo $singular_name; ?></a>
</h2>


<div class="form newentry_form" id="O2M_new_<?php echo $plural_name ?>">
	<table style="position:relative;" border="0" cellspacing="0" cellpadding="3" width="96%">
	<?php
		df_make_form($table_info, $current_doc_id, 'new', '', $plugin_cell);
	?>
	</table>
	<div style="margin-top: 1em; clear: both; text-align:center;">
		<button style="margin: 5px; padding: 5px;" onclick="clear_newEntry_form();return true;" id="submit_new_<?php echo $plural_name ?>" type="submit">
			Add new <?php echo $singular_name; ?>
		</button>
	</div>
</div>

<div id="error_field_<?php echo $plural_name ?>" style="background: 1px solid #500;"></div>




<h2> &nbsp; Current <?php echo $plural_name; ?>:</h2>

<table cellspacing="0" cellpadding="0" class="dftable" id="O2M_<?php echo $plural_name ?>">	
	<?php
		//TODO: paginate?
		//TODO: support multiple tabs w/ tiny MCEs?
		df_list_table($table_info, $current_doc_id, $plugin_cell);
	?>
</table>


<script type="text/javascript">

//New Entry:

var power_table = new df_O2M_table(j("#O2M_<?php echo $plural_name ?>"));


//show/hide new entry div
j("#O2M_new_<?php echo $plural_name ?>").hide().css("height","0px");

j("#newentry_header_<?php echo $plural_name ?>").toggle(function(){
		j("#O2M_new_<?php echo $plural_name ?>").show().css("height","auto");
	},
	function(){
		j("#O2M_new_<?php echo $plural_name ?>").hide().css("height","0px");
	}
);


//submit new entry for creation:
j("#submit_new_<?php echo $plural_name ?>").click(function(e){
			
	modify_entry(j("#O2M_new_<?php echo $plural_name ?>"), 'create', function(resp){
		responseData =  eval( '(' + resp + ')') ;
		if (responseData.status == '200'){
			power_table.addFullRow(responseData.rowData);
		}

	});
	
	return false;
});


// Edit entry:

var entry_id, modal = new modal_window("edit_modal");

j("#O2M_<?php echo $plural_name ?> .clickable").live('click', function(e){
	
	entry_id = j(e.target.parentNode).attr('title');
	
	j("#editentry_form").html('<h2>Loading entry ' + entry_id + '...</h2>');
	modal.show();
		var dataString = 'entryid=' + entry_id + '&plugin_cell=<?php echo $plugin_cell; ?>&current_doc_id=<?php echo $current_doc_id ?>&table_info=<?php
			echo urlencode(serialize($table_info)); ?> ?>';
		jQuery.ajax({
			type: "POST",
			url: "../assets/plugins/dataFrolic/df_get_entry.php",
			data: dataString,
			success: function(resp){
				if (resp == 'false'){
					resp = "Database Error.  Please try again or contact your webadmin for assistance."
				}
				j("#editentry_form").html(resp);
			}
		});
	
	
});
</script>

